<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">
  <style>
    body {
      padding: 50px;
    }
  </style>

</head>

<body>
  <div class="table-responsive container">
    <table class="table table-bordered  table-hover" id="table">
      <tr>
        <th class="col-md-1">序号</th>
        <th class="col-md-1">题目</th>
        <th class="col-md-3">内容</th>
        <th class="col-md-1">作者</th>
      </tr>
    </table>
  </div>
  <script src="data.js"></script>
  <script>
    var table = document.getElementById('table');
    jsonData = JSON.parse(data);

    // for(var i = 0; i < jsonData.length; i++) {
    //     var tr = table.insertRow();

    //     td = tr.insertCell();
    //     td.innerHTML = i + 1;

    //     td = tr.insertCell();
    //     td.innerHTML = jsonData[i].title;

    //     td = tr.insertCell();
    //     td.innerHTML = jsonData[i].content.replace(/\|/g, "<br/>");

    //     td = tr.insertCell();
    //     td.innerHTML = jsonData[i].authors;
    // }

    for (var i = 0; i < jsonData.length; i++) {
      //在 ES6 之前，字符串内夹杂变量，我们总是通过分离和+号连接解决
      // table.innerHTML +=
      //   "<tr>" +
      //   "<td>" + (i + 1) + "</td>" +
      //   "<td>" + jsonData[i].title + "</td>" +
      //   "<td>" + jsonData[i].content.replace(/\|/g, "<br/>") + "</td>" +
      //   "<td>" + jsonData[i].authors + "</td>" +
      //   "</tr>"


      //在ES6中直接使用(`)反引号配合${var}模版语法格式，直接实现变量解析功能
      table.innerHTML += `
                  <tr>
                     <td>${i + 1}</td>
                     <td>${jsonData[i].title}</td>
                     <td>${jsonData[i].content.replace(/\|/g, "<br/>")}</td>
                     <td>${jsonData[i].authors}</td>
                  </tr>
                  `;

    }

  </script>
</body>

</html>